package com.service.impl;

import com.beans.Comment;
import com.beans.DataJson;
import com.beans.ResponseResult;
import com.vo.CommentVo;
import com.vo.ForumVo;
import com.mapper.CommentMapper;
import com.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Transactional
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public int countCommentByForumId(ForumVo forumVo) {
        return commentMapper.countCommentByForumId(forumVo);
    }

    @Override
    public List<CommentVo> listByForumId(int id) {
        return commentMapper.listByForumId(id);
    }

    @Override
    public ResponseResult addComment(Comment comment) {

        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        comment.setCreateTime(timestamp);

        int res = commentMapper.addComment(comment);

        if(res <= 0){
            return ResponseResult.error("评论失败");
        }
        return ResponseResult.success("评论成功");
    }

    @Override
    public ResponseResult deleteComment(int id) {
        int res = commentMapper.deleteComment(id);
        if(res <= 0){
            return ResponseResult.error("删除失败");
        }
        return ResponseResult.success("删除评论成功");
    }

    @Override
    public List<CommentVo> getAllComment() {
        return commentMapper.getAllComment();
    }
}
